package com.dabai.iot.core.controller;

import cn.hutool.core.lang.tree.Tree;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.dabai.common.core.domain.R;
import com.dabai.common.satoken.utils.LoginHelper;
import com.dabai.common.web.core.BaseController;
import com.dabai.iot.core.convert.DeviceGroupConvert;
import com.dabai.iot.core.dto.DeviceGroupBind;
import com.dabai.iot.core.dto.DeviceGroupInput;
import com.dabai.iot.core.dto.DeviceGroupUpdate;
import com.dabai.iot.core.entity.DeviceGroupEntity;
import com.dabai.iot.core.service.IDeviceGroupService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * 设备分组控制器
 *
 * @author kev1n
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/deviceGroup")
public class DeviceGroupController extends BaseController {

    private final IDeviceGroupService deviceGroupService;

    /**
     * 新增分组
     */
    @PostMapping("/save")
    @Operation(summary = "新增分组")
    public R<Void> save(@Valid @RequestBody DeviceGroupInput input) {
        DeviceGroupEntity deviceGroup = Objects.requireNonNull(DeviceGroupConvert.INSTANCE.convert(input));
//        deviceGroup.setTenantId(LoginHelper.getTenantId());
//        return RP.status(deviceGroupService.saveDeviceGroup(deviceGroup));

        return toAjax(deviceGroupService.addDeviceGroup(deviceGroup));
    }

    /**
     * 修改分组
     */
    @PostMapping("/update")
    @Operation(summary = "修改分组")
    public R<Void> update(@Valid @RequestBody DeviceGroupUpdate update) {
        DeviceGroupEntity deviceGroup = deviceGroupService.getById(update.getId());
        if (deviceGroup == null) {
            return R.fail("分组不存在");
        }

        deviceGroup.setParentId(update.getParentId());
        deviceGroup.setGroupName(update.getGroupName());
        deviceGroup.setGroupDesc(update.getGroupDesc());
        deviceGroup.setSort(update.getSort());
        return toAjax(deviceGroupService.updateDeviceGroup(deviceGroup));
    }

    /**
     * 删除分组
     */
    @PostMapping("/remove")
    @Operation(summary = "删除分组", description = "传入主键id")
    public R<Void> remove(@Parameter(description = "主键id", required = true) @RequestParam Long id) {
        return toAjax(deviceGroupService.removeDeviceGroup(id));
    }

    /**
     * 设备分组详情
     */
    @GetMapping("/detail")
    @Operation(summary = "设备分组详情", description = "传入主键id")
    public R<DeviceGroupEntity> detail(@RequestParam Long id) {
        DeviceGroupEntity detail = deviceGroupService.getById(id);
        return R.ok(detail);
    }

    /**
     * 分组下拉数据源
     */
    @GetMapping("/select")
    @Operation(summary = "分组下拉数据源", description = "下拉数据源，用于设备分组选择")
    public R<List<DeviceGroupEntity>> select() {
        List<DeviceGroupEntity> list = deviceGroupService.list(Wrappers.<DeviceGroupEntity>query().lambda().eq(DeviceGroupEntity::getTenantId, LoginHelper.getTenantId()));
        return R.ok(list);
    }

    /**
     * 获取分组树形结构
     */
    @GetMapping("/tree")
    @Operation(summary = "树形结构")
    public R<List<Tree<Long>>> tree() {
        return R.ok(deviceGroupService.tree(LoginHelper.getTenantId()));
    }

    /**
     * 绑定设备
     */
    @PostMapping("/bindDevice")
    @Operation(summary = "绑定设备")
    public R<Void> bindDevice(@Valid @RequestBody DeviceGroupBind entity) {
        return toAjax(deviceGroupService.bindDevice(entity));
    }

    /**
     * 解绑设备
     */
    @PostMapping("/unbindDevice")
    @Operation(summary = "解绑设备")
    public R<Void> unbindDevice(@Valid @RequestBody DeviceGroupBind entity) {
        return toAjax(deviceGroupService.unbindDevice(entity));
    }

}
